package com.mrd.gtimpframe.service.impl;

import com.mrd.base.Jqgrid;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimpframe.entity.DictionaryGroup;
import com.mrd.gtimpframe.repository.DictionaryGroupMapper;
import com.mrd.gtimpframe.service.DictionaryGroupService;
import com.mrd.gtimpframe.vo.DictionaryGroupVO;
import com.mrd.redis.JedisClientClusterImpl;
import com.mrd.util.LoginUtil;
import com.mrd.util.StringUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class DictionaryGroupServiceImpl implements DictionaryGroupService {
    @Autowired
    private DictionaryGroupMapper dictionaryGroupMapper;
    @Autowired
    private JedisClientClusterImpl jedisClientCluster;

    @Override
    public int deleteByPrimaryKey(String id) {
        int i = dictionaryGroupMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            updateRedis();
        }
        return i;
    }

    @Override
    public int insert(DictionaryGroup record) {
        int i = dictionaryGroupMapper.insert(record);
        if (i > 0) {
            updateRedis();
        }
        return i;
    }

    @Override
    public int insertSelective(DictionaryGroup record) {
        int i = dictionaryGroupMapper.insertSelective(record);
        if (i > 0) {
            updateRedis();
        }
        return i;
    }

    @Override
    public DictionaryGroup selectByPrimaryKey(String id) {
        List<DictionaryGroup> groups = this.selectAll(true);
        for (DictionaryGroup group : groups) {
            if (id.equals(group.getId())) {
                return group;
            }
        }
        return dictionaryGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public DictionaryGroup selectByCode(String code) {
        List<DictionaryGroup> groups = this.selectAll(true);
        for (DictionaryGroup group : groups) {
            if (code.equals(group.getCode())) {
                return group;
            }
        }
        return dictionaryGroupMapper.selectByCode(code);
    }

    @Override
    public int updateByPrimaryKeySelective(DictionaryGroup record) {
        int i = dictionaryGroupMapper.updateByPrimaryKeySelective(record);
        if (i > 0) {
            updateRedis();
        }
        return i;
    }

    @Override
    public int updateByPrimaryKey(DictionaryGroup record) {
        int i = dictionaryGroupMapper.updateByPrimaryKey(record);
        if (i > 0) {
            updateRedis();
        }
        return i;
    }

    @Override
    public List<DictionaryGroup> selectAll(boolean justValid) {
        List<DictionaryGroup> dictionaryGroupList = null;
        try {
            if (justValid) {
                dictionaryGroupList = jedisClientCluster
                        .range("dictionaryGroup", 0, -1, DictionaryGroupVO.class);
            }
        }catch (Exception e){

        }
        if (dictionaryGroupList == null || dictionaryGroupList.size() == 0) {
            List<DictionaryGroupVO> uiList = dictionaryGroupMapper.selectAllForView(justValid);
            dictionaryGroupList = new ArrayList<>();
            for (DictionaryGroupVO uiDictionaryGroup : uiList) {
                dictionaryGroupList.add(uiDictionaryGroup);
            }
            try {
                jedisClientCluster.rightPushAll("dictionaryGroup", dictionaryGroupList, null, TimeUnit.SECONDS);
            }catch (Exception e){

            }
        }

        return dictionaryGroupList;
    }

    @Override
    public Page<DictionaryGroup> pageListData(Jqgrid jqgrid) {
        // 分页
        jqgrid.pageWithMybatis();

        // 排序
        jqgrid.sortWithMybatis();

        //过滤
        Map<String, Object> filterMap = jqgrid.getFilterMap();
        return jqgrid.pageResult(dictionaryGroupMapper.selectByMap(filterMap));
    }

    @Override
    public List<DictionaryGroup> dropdownList(String key) {
        List<DictionaryGroup> allGroup = dictionaryGroupMapper.selectAll(true);
        List<DictionaryGroup> targetGroupList = new ArrayList<>();
        for (DictionaryGroup group : allGroup) {
            if(StringUtils.isNotEmpty(key)){
                String gName = group.getName();
                gName = StringUtils.isEmpty(gName) ? "" : gName;
                String gCode = group.getCode();
                gCode = StringUtils.isEmpty(gCode) ? "" : gCode;
                if(gName.contains(key) || gName.equals(key) || gCode.contains(key) || gCode.equals(key)){
                    targetGroupList.add(group);
                }
                System.err.println(key);
            } else {
                targetGroupList.add(group);
            }
        }
        return targetGroupList;
    }

    @Override
    public Result saveOrUpdate(DictionaryGroup dtg) {
        if (dtg != null) {
            if (StringUtils.isNotEmpty(dtg.getId())) {
                dtg.setModifyTime(new Date());
                dtg.setModifier(LoginUtil.getLoginAccount());
                updateByPrimaryKeySelective(dtg);
            } else {
                dtg.setCreator(LoginUtil.getLoginAccount());
                dtg.setCreateTime(new Date());
                dtg.setId(StringUtils.getUUID());
                insert(dtg);
            }
        } else {
            return Result.newFailInstance("错误的操作");
        }
        return Result.newSuccessInstance("已保存", dtg.getId());
    }

    private void updateRedis() {
        jedisClientCluster.delete("dictionaryGroup");
        this.selectAll(true);
//        jedisClientCluster.rightPushAll("dictionaryGroup", , null, TimeUnit.SECONDS);
    }
}
